package com.bw.a2207a.view.fragment

import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseFragment
import com.bw.a2207a.bean.GoodsBean
import com.bw.a2207a.bean.StudentInfoBean
import com.bw.a2207a.databinding.FragmentHomeBinding
import com.bw.a2207a.db.CartDbUtil
import com.bw.a2207a.view.adapter.GoodsAdapter
import com.bw.a2207a.viewmodel.HomeViewModel
import com.jcodecraeer.xrecyclerview.XRecyclerView.LoadingListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * A simple [Fragment] subclass.
 * Use the [HomeFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(R.layout.fragment_home) {
    var page = 1
    val size = 10

    lateinit var adapter: GoodsAdapter
    val datas = mutableListOf<GoodsBean>()

    override fun initData() {
        viewModel.bannerLiveData.observe(this) {
            if (it.code == 200) {
                binding.banner.setData(it.data, null)
                binding.banner.setmAdapter { banner, model, view, position ->
                    Glide.with(this).load(it.data.get(position).imagePath).into(view as ImageView)
                }
            }
        }

        viewModel.goodsLiveData.observe(this) {
            if (it.code == 200) {
                if (page == 1) {
                    datas.clear()
                }
                datas.addAll(it.data)
                adapter.notifyDataSetChanged()

                //处理刷新完成
                binding.srl.finishRefresh()
                binding.srl.finishLoadMore()

            }
        }
    }

    override fun initView() {
//        CartDbUtil.cartDb.getStudentDao().insertStudent(StudentInfoBean(null,"ddd",33,"男"))
//
//        CartDbUtil.cartDb.getStudentDao().getStudents()
        //发送网络请求，获取banner，获取商品列表
        viewModel.getBanners()

        getGoodsData()

        binding.rv.layoutManager = GridLayoutManager(activity, 2)
        adapter = GoodsAdapter(R.layout.item_goods_layout, datas)
        binding.rv.adapter = adapter


        adapter.addChildClickViewIds(R.id.tv_add)
        adapter.setOnItemChildClickListener { adapter, view, position ->
            //添加购物车
            viewModel.addCar(datas.get(position).id)
        }



        adapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance()
                .build("/main/GoodsDetailActivity")
                .withInt("id", datas.get(position).id)
                .navigation()
        }

//        binding.srl.setOnLoadMoreListener(object :OnLoadMoreListener{
//            override fun onLoadMore(refreshLayout: RefreshLayout) {
//            }
//
//        })
//
//        binding.srl.setOnRefreshListener {
//
//        }
        binding.srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                //刷新；请求第一页数据
                page = 1
                viewModel.getGoods(0, page, size)
                //  getGoodsData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //加载更多，请求下一页数据
                viewModel.getGoods(0, page++, size)
                //getGoodsData()
            }

        })

        binding.etSearch.setOnClickListener {
            ARouter.getInstance().build("/main/SearchActivity").navigation()
        }

    }


    fun getGoodsData() {
        viewModel.getGoods(0, page, size)
    }

}